﻿Imports System.Data.OleDb
Public Class DataProvider
    Public Shared cnStr As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = QuanLyNhaSach.mdb"

    Public Shared Function Open() As OleDbConnection
        Dim connect As New OleDbConnection(cnStr)
        connect.Open()
        Return connect
    End Function

    Public Shared Sub Close(ByVal connect As OleDbConnection)
        If Not (connect Is Nothing) AndAlso connect.State = ConnectionState.Open Then
            connect.Close()
        End If
    End Sub

    Public Shared Function ExcuteNonQuery(ByVal sql As String) As Integer
        Dim cn As OleDbConnection = Open()
        Dim cmd As New OleDbCommand(sql, cn)
        Dim dr As Integer
        dr = cmd.ExecuteNonQuery()
        Close(cn)
        Return dr
    End Function

    Public Shared Function ExcuteReader(ByVal sql As String) As DataTable
        Dim cn As OleDbConnection = Open()
        Dim adp As New OleDbDataAdapter(sql, cn)
        Dim tb As New DataTable()
        adp.Fill(tb)
        Close(cn)
        Return tb
    End Function

    Public Shared Function ExcuteScalar(ByVal sql As String) As Object
        Dim cn As OleDbConnection = Open()
        Dim cmd As New OleDbCommand(sql, cn)
        Dim dr As Object
        dr = cmd.ExecuteScalar()
        Close(cn)
        Return dr
    End Function

End Class
